package com.af.community.smart.business

import android.os.Bundle
import com.af.community.smart.base.*
import com.af.community.smart.fragment.OnFragmentListener
import com.af.community.smart.fragment.PeopleDetail1Fragment
import com.af.community.smart.fragment.PeopleDetail2Fragment
import com.af.community.smart.model.PeopleData
import com.af.community.smart.utils.bindLifeCycle
import com.af.community.smart.utils.subscribeByFinally
import com.af.community.smart.viewmodel.VerifyViewModel
import kotlinx.android.synthetic.main.activity_patrol_people_detial.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class PatrolPeopleDetialActivity : BaseActivity(), OnFragmentListener {

    private lateinit var peopleType: String
    private var mData: PeopleData? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_patrol_people_detial)
        initTitle(title = "详情", name = "编辑")

        getViewModel().getPeopleData(intent.getStringExtra("id").orEmpty())
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mData = it
                    if (it.gridId == getString("gridId")) {
                        tvRight?.visible()
                    }

                    when (peopleType) {
                        "1" -> {
                            supportFragmentManager.beginTransaction()
                                .add(
                                    R.id.verify_container,
                                    PeopleDetail1Fragment().apply {
                                        arguments =
                                            Bundle().apply { putSerializable("data", mData) }
                                    }
                                )
                                .commit()
                        }
                        "2" -> {
                            supportFragmentManager.beginTransaction()
                                .add(
                                    R.id.verify_container,
                                    PeopleDetail2Fragment().apply {
                                        arguments =
                                            Bundle().apply { putSerializable("data", mData) }
                                    }
                                )
                                .commit()
                        }
                    }
                }
            )
    }

    override fun getViewModel() = getViewModel<VerifyViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        tvRight?.gone()
        peopleType = intent.getStringExtra("type")

        tvRight?.oneClick {
            if (mData == null
                || mData!!.account.isNullOrEmpty()
            ) {
                toast("人口信息获取失败！")
                return@oneClick
            }

            when (peopleType) {
                "1" -> startActivity<PeopleEditFirstActivity>("data" to mData)
                "2" -> startActivity<PeopleEditSecondActivity>("data" to mData)
            }
        }
        bt_add.oneClick {
            if (mData == null
                || mData!!.account.isNullOrEmpty()
            ) {
                toast("人口信息获取失败！")
                return@oneClick
            }

            startActivity<PatrolAddActivity>("people" to mData)
        }
    }

    override fun onDataChanged(item: Any?) {
        if (item !== null && item is PeopleData) mData = item
    }

}
